package com.xiangban.data_platform.controller;

import com.xiangban.data_platform.domain.User;
import com.xiangban.data_platform.domain.dto.*;
import com.xiangban.data_platform.service.QuenstionnaireService;
import com.xiangban.data_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author Mr. Ma
 * @Data 2020/12/2 12:53
 */
@RestController
@RequestMapping("/api/v1/quenstionnaire")
@Api(tags = "问卷调查管理")
public class QuenstionnaireController {

    @Autowired
    private QuenstionnaireService quenstionnaireService;

    @GetMapping("/findQuenstionAnswer")
    @ApiOperation("查询标题和选项列表")
    public JsonData QuenstionAnswer() {

        List<Quenstions> quenstionAnswer = quenstionnaireService.findQuenstionAnswer();

        return JsonData.buildSuccess(quenstionAnswer);
    }

    @PostMapping("/findQuenstionire")
    @ApiOperation("查询已选中选项")
    public JsonData Quenstionire(@RequestParam(value = "user_id", required = true) int userId) {

        List<Quenstionnaire> beChosen = (List<Quenstionnaire>) quenstionnaireService.findBeChosen(userId);
        return JsonData.buildSuccess(beChosen);
    }

    @PostMapping("/UserAndQuenstionAnswer")
    @ApiOperation("问卷修改")
    public JsonData UserAndQuenstionAnswer(@RequestBody Map<String, Object> map) {
        quenstionnaireService.saveQuenstionnaire(map);
        return JsonData.buildSuccess();
    }

    @PostMapping("/selectAnswer")
    @ApiOperation("根据id查询所有选项")
    public JsonData selectAnswer(@RequestBody Answer answer) {

        return JsonData.buildSuccess(quenstionnaireService.selectAnswer(answer));
    }

    @PostMapping("/selectAnswerById")
    @ApiOperation("根据选项id查询所有选项")
    public JsonData selectAnswerById(@RequestBody AnswerByAnswerId answer) {
        return JsonData.buildSuccess(quenstionnaireService.selectAnswerById(answer));
    }

    @PostMapping("/saveUserQuenstionnaire")
    @ApiOperation("(客服系统)添加用户信息和问卷")
    public JsonData saveUserQuenstionnaire(@RequestBody Map<String, Object> map) {
        int i = quenstionnaireService.saveUserQuenstionnaire(map);
        if (i!=1){
            return JsonData.buildError("添加失败，信息重复");
        }else {
            return JsonData.buildSuccess(quenstionnaireService.saveUserQuenstionnaire(map));
        }
    }

    @PostMapping("/updateAnswer")
    @ApiOperation("(客服系统)修改问卷选项值")
    public JsonData updateAnswer(@RequestBody Answer answer) {
        int i = quenstionnaireService.updateAnswer(answer);
        if (i!=1){
            return JsonData.buildError("修改失败,");
        }else {
            return JsonData.buildSuccess("修改成功");
        }
    }

    @PostMapping("/saveAnswer")
    @ApiOperation("(客服系统)添加问卷选项值")
    public JsonData saveAnswer(@RequestBody Answer answer) {
        int i = quenstionnaireService.saveAnswer(answer);
        if (i!=1){
            return JsonData.buildError("添加失败,");
        }else {
            return JsonData.buildSuccess("添加成功");
        }
    }

    @PostMapping("/deleteAnswer")
    @ApiOperation("(客服系统)删除问卷选项值")
    public JsonData deleteAnswer(@RequestBody Answer answer){
        int i = quenstionnaireService.deleteAnswer(answer);
        if (i!=1){
            return JsonData.buildError("删除失败,无id");
        }else {
            return JsonData.buildSuccess("删除成功");
        }
    }

    @PostMapping("/findAnswerById")
    @ApiOperation("(客服系统)根据类别id查询类别")
    public JsonData findAnswerById(@RequestBody Answer answer){

        return JsonData.buildSuccess(quenstionnaireService.findAnswerById(answer));

    }
}
